
let time='2019-05-24 23:16:14'

const datetime=(time,char='.')=>{
    if (typeof(time)=='string'){
        time = time.replace(/\-/g, "/")
      }
      const date = new Date(time);
      const Y = date.getFullYear()
      const M = formatNumber(date.getMonth())
      const D = formatNumber(date.getDate())
      const h = formatNumber(date.getHours())
      const m = formatNumber(date.getMinutes())
      const s = formatNumber(date.getSeconds())
      return {
        YMD: Y + char + M + char + D,
        MD: M + '月' + D + '日',
        hm: h + ":" + m,
        all: M + '/' + D + " " + h + ":" + m,
        real:getReal(date)
      }
}

const formatNumber = n => {
    n = n.toString()
    return n[1] ? n : '0' + n
  }


const getReal = date => {
    let day=2
    const nowDate=new Date()
    const nowYear=nowDate.getFullYear()
    const Y=date.getFullYear()
    const M = formatNumber(date.getMonth()+1)
    const D = formatNumber(date.getDate())
    const h = formatNumber(date.getHours())
    const m = formatNumber(date.getMinutes())
    const s = formatNumber(date.getSeconds())
    const zeroDate=new Date(Y,M-1,D);
    const dayNum=60*60*1000*24
    const difference=nowDate-zeroDate;
    if(Y<nowYear){
        return `${Y}年${M}月${D}日 ${h}:${m}`
    }
    if(difference<(day+1)*dayNum){
        let beforeDay=parseInt(difference/dayNum)
        if(difference/dayNum<0){
            return `${Y}年${M}月${D}日 ${h}:${m}`
        }
        if(beforeDay==0){
            return `今天 ${h}:${m}`
        }
        if(beforeDay==1){
            return `昨天 ${h}:${m}`
        }
        if(beforeDay==2){
            return `前天 ${h}:${m}`
        }
    }
    return `${M}月${D}日 ${h}:${m}`
  }
